﻿/******************************************************************************************
*                                                                                         *    
*                              Developed by Bruno Pires                                   *
*                                                                                         *
*                             email: bruno@blastersystems.com                             *
*                               web: www.blastersystems.com                               *
*                              blog: www.blastersystems.com/blog                          *
*                           twitter: brunoacpires                                         *
*                                                                                         *
*                      Software available under GNU LGPL License                          *
*                                                                                         *    
* *****************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EFDataPager.UserControls
{
    public partial class ControloDataPager : System.Web.UI.UserControl
    {
        #region Properties
        public int PageSize { get; set; }                  /*número de registos por página*/           /*number of records per page*/
        public int RowCount
        {
            get
            {
                return (int)ViewState["RowCount"];
            }
            set
            {
                ViewState["RowCount"] = value;
            }
        }                            /*número total de registos */               /* total number of records*/
        public int CurrentRow
        {
            get
            {
                return (int)ViewState["CurrentRow"];
            }
            set
            {
                ViewState["CurrentRow"] = value;
            }
        }                            /*número do primeiro registo apresentado*/  /* first record in current page*/
        public int CurrentPage
        {
            get
            {
                if (ViewState["CurrentPage"] != null)
                    return (int)ViewState["CurrentPage"];
                else
                    return 0;
            }
            set
            {
                ViewState["CurrentPage"] = value;
            }
        }                            /*página actual                             /*current page number*/
        public int PageCount
        {
            get
            {
                return (int)ViewState["PageCount"];
            }
            set
            {
                ViewState["PageCount"] = value;
            }
        }                            /*número total de páginas*/                 /*total number of pages*/
        #endregion

        #region Events and Handlers
        public delegate void ListViewPagerNextHandler(object sender, PagerEventArgs e);
        public delegate void ListViewPagerPreviousHandler(object sender, PagerEventArgs e);
        public delegate void ListViewPagerFirstHandler(object sender, PagerEventArgs e);
        public delegate void ListViewPagerLastHandler(object sender, PagerEventArgs e);

        public event ListViewPagerNextHandler Next;
        public event ListViewPagerPreviousHandler Previous;
        public event ListViewPagerFirstHandler First;
        public event ListViewPagerLastHandler Last;


        protected void OnNext(int _pageSize, int _rowCount, int _currentRow, int _currentPage, int _pageCount)
        {
            var args = new PagerEventArgs(_pageSize, _rowCount, _currentRow, _currentPage, _pageCount);
            Next(this, args);
        }

        protected void OnPrevious(int _pageSize, int _rowCount, int _currentRow, int _currentPage, int _pageCount)
        {
            var args = new PagerEventArgs(_pageSize, _rowCount, _currentRow, _currentPage, _pageCount);
            Previous(this, args);
        }

        protected void OnFirst(int _pageSize, int _rowCount, int _currentRow, int _currentPage, int _pageCount)
        {
            var args = new PagerEventArgs(_pageSize, _rowCount, _currentRow, _currentPage, _pageCount);
            First(this, args);
        }

        protected void OnLast(int _pageSize, int _rowCount, int _currentRow, int _currentPage, int _pageCount)
        {
            var args = new PagerEventArgs(_pageSize, _rowCount, _currentRow, _currentPage, _pageCount);
            Last(this, args);
        }
        #endregion

        #region Events
        protected void FirstPage_Click(object sender, ImageClickEventArgs e)
        {
            CurrentRow = 0;
            CurrentPage = 0;
            TestActiveSettings();
            OnFirst(PageSize, RowCount, 0, CurrentPage, PageCount);
        }

        protected void PreviousPage_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage--;

            TestActiveSettings();

            var res = CurrentRow - PageSize;

            if (res >= 0)
            {
                CurrentRow = res;
                OnPrevious(PageSize, RowCount, res, CurrentPage, PageCount);
            }
        }

        protected void NextPage_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage++;
            TestActiveSettings();

            var res = CurrentRow + PageSize;

            if (res < RowCount)
            {
                CurrentRow = res;
                OnNext(PageSize, RowCount, res, CurrentPage, PageCount);
            }
        }

        protected void LastPage_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage = PageCount;
            TestActiveSettings();

            int nRows = RowCount % PageSize;
            int LastPageCurrentRow = RowCount - nRows;
            CurrentRow = LastPageCurrentRow;
            OnLast(PageSize, RowCount, LastPageCurrentRow, CurrentPage, PageCount);

        }
        #endregion

        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FirstPage.Visible = false;
                PreviousPage.Visible = false;
                NextPage.Visible = true;
                LastPage.Visible = true;
            }

            CalculateNumberOfPages();
        }
        #endregion

        #region Methods
        private void TestActiveSettings()
        {
            if (CurrentPage == 0)
            {
                FirstPage.Visible = false;
                PreviousPage.Visible = false;
                NextPage.Visible = true;
                LastPage.Visible = true;
            }
            else if (CurrentPage == PageCount)
            {
                FirstPage.Visible = true;
                PreviousPage.Visible = true;
                NextPage.Visible = false;
                LastPage.Visible = false;
            }
            else
            {
                FirstPage.Visible = true;
                PreviousPage.Visible = true;
                NextPage.Visible = true;
                LastPage.Visible = true;
            }
        }

        private void CalculateNumberOfPages()
        {
            int nRowsMod = RowCount % PageSize;

            if (nRowsMod == 0)
            {
                PageCount = RowCount / PageSize;
                PageCount--;
            }
            else
            {
                PageCount = RowCount / PageSize;
            }
        }
        #endregion
    }
}